package com.songxh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songxh.common.R;
import com.songxh.dto.DishDto;
import com.songxh.dto.SetmealDto;
import com.songxh.pojo.Category;
import com.songxh.pojo.Dish;
import com.songxh.pojo.Setmeal;
import com.songxh.pojo.SetmealDish;
import com.songxh.service.CategoryService;
import com.songxh.service.DishService;
import com.songxh.service.SetmealDishService;
import com.songxh.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author songxh
 * @create 2022-04-26-10:26
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        QueryWrapper<Setmeal> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.orderByDesc("update_time");
        setmealService.page(pageInfo,queryWrapper);

        //获取套餐分类的名字
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list=new ArrayList<>();
        for (Setmeal record : records) {
            //可以参考SetmealDto的属性
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(record,dto);

            //获取分类的名字并将该名字赋值给dto
            Long categoryId = dto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dto.setCategoryName(categoryName);
            list.add(dto);
        }
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }


    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }


    /**
     * 对套餐实现启售和停售
     * @param ids  套餐id的集合
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@RequestParam List<Long> ids,@PathVariable int status){
        UpdateWrapper<Setmeal> wrapper=new UpdateWrapper<>();
        wrapper.in("id",ids);
        wrapper.set("status",status);
        setmealService.update(wrapper);
        return R.success("修改成功");
    }


    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeByIds(ids);
        QueryWrapper<SetmealDish> queryWrapper=new QueryWrapper<>();
        for (Long id : ids) {
            queryWrapper.eq("setmeal_id",id);
            setmealDishService.remove(queryWrapper);
        }
        return R.success("删除成功");
    }


    /**
     * 修改套餐时回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getByIdWithDish(@PathVariable long id){
        SetmealDto setmealDto=setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }


    /**
     * 前台首页展示套餐时会用上
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> list(Setmeal setmeal){
        QueryWrapper<Setmeal> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category_id",setmeal.getCategoryId());
        queryWrapper.eq("status",setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        if (list==null){
            return R.error("该分类没有对应的套餐");
        }

        //创建一个集合来存放SetmealDto
        List<SetmealDto> dtoList=new ArrayList<>();

        for (Setmeal set : list) {
            SetmealDto dto = new SetmealDto();

            //将该套餐所有的数据拷贝到dto中
            BeanUtils.copyProperties(set,dto);

            //获取CategoryName
            Long categoryId = set.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dto.setCategoryName(categoryName);

            //获取setmealDishes
            QueryWrapper<SetmealDish> dishQueryWrapper=new QueryWrapper<>();
            Long setmealId = set.getId();
            dishQueryWrapper.eq("setmeal_id",setmealId);
            List<SetmealDish> dishList = setmealDishService.list(dishQueryWrapper);
            dto.setSetmealDishes(dishList);

            dtoList.add(dto);
        }

        return R.success(dtoList);
    }


    //setmeal/dish/1518859071678181378
    @GetMapping("/dish/{id}")
    public R<List<Dish>> dish(@PathVariable long id){
        QueryWrapper<SetmealDish> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        List<Dish> dishList=new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            dishList.add(dish);
        }

        return R.success(dishList);
    }

}
